package hu.mapro.gwtui.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;


public class GwtUtils {

	public static <T> AsyncCallback<T> toCallback(final Receiver<T> receiver) {
		return new AsyncCallback<T>() {

			@Override
			public void onFailure(Throwable caught) {
				receiver.onFailure(new ServerFailure(caught.getMessage()));
			}

			@Override
			public void onSuccess(T result) {
				receiver.onSuccess(result);
			}
		};
	}
	
	public static <T> Receiver<T> toReceiver(final AsyncCallback<T> receiver) {
		return new Receiver<T>() {

			@Override
			public void onFailure(ServerFailure error) {
				receiver.onFailure(new RuntimeException(error.getMessage()));
			}
			
			@Override
			public void onSuccess(T result) {
				receiver.onSuccess(result);
			}
		};
	}
	
}
